#include <SFML/OpenGL.hpp>
#include "Particle.h"

#include <random>

using namespace std;

Particle::Particle(GameState *gameState) : PhysicalObject(gameState){
	getBodyDef()->type = b2_dynamicBody;
	getBodyDef()->fixedRotation = false;
	b2FixtureDef *fixDef = new b2FixtureDef;
	b2PolygonShape *shape = new b2PolygonShape;
	shape->SetAsBox(0.5f, 0.5f);
	fixDef->shape = shape;
	fixDef->density = 1.0;
	fixDef->restitution = 0.3f;
	fixDef->isSensor = true;
	addFixtureDef(fixDef);
	setRotation(0.0f);

	float randCol = (rand()%20)/100.0f;
	setColor(0.4f+randCol, 0.6f+randCol, 0.8f+randCol);


}


Particle::~Particle()
{
}


void Particle::draw(){
	b2Vec2 position = getPosition();
	float rotation = m_body->GetAngle();
	b2Vec2 size = getSize();

	glColor4fv(m_color);
	glPushMatrix();
	{
		glTranslatef(position.x,position.y,0);
		glRotatef(rotation*57.2957795,0,0,1);
		glScalef(size.x, size.y, 1.0f);
		glBegin(GL_TRIANGLES);
		{			
			glTexCoord2f(0.0f, 1.0f); glVertex3f( -0.5f, -0.5f,  0.0f);
			glTexCoord2f(1.0f, 1.0f); glVertex3f( 0.5f, -0.5f,  0.0f);
			glTexCoord2f(1.0f, 0.0f); glVertex3f( 0.5f, 0.5f,  0.0f);
		}
		glEnd();
	}
	glPopMatrix();


}

void Particle::update(double dt){
}

void Particle::beginContact(b2Contact *contact){
	PhysicalObject *me = (PhysicalObject*)contact->GetFixtureA()->GetBody()->GetUserData();
	PhysicalObject *other = (PhysicalObject*)contact->GetFixtureB()->GetBody()->GetUserData();

	if(me != this){ 
		PhysicalObject *temp;
		temp = me;
		me = other;
		other = temp;
	}

	if(other->hasProperty("floor")){
		kill();
	}


}